package cn.doctorpda.conference.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.collection.CollectionInfoItem;
import cn.doctorpda.conference.utils.ImageUtils;
import cn.doctorpda.conference.utils.ScreenUtils;

/**
 * Created by huangjinlong on 2016/3/29.
 */
public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.ViewHolder> {

    //    private List<String> datas = new ArrayList<>();
    private ArrayList<CollectionInfoItem> collectionList = new ArrayList<CollectionInfoItem>();
    private OnItemClickListener listener;
    private Context context;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, CollectionInfoItem data);
    }


    public void clear() {
        this.collectionList.clear();
        notifyDataSetChanged();
    }

    public void addAll(ArrayList<CollectionInfoItem> collectionList) {
        this.collectionList.addAll(collectionList);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collection, parent, false);
        LinearLayout.LayoutParams la = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(parent.getContext()) / 2, ScreenUtils.getScreenHeight(parent.getContext()) / 4);
        view.setLayoutParams(la);
        this.context = parent.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final int pos = position;
        final CollectionInfoItem itemInfo = collectionList.get(pos);

        if (itemInfo.getTHUMB() != null && !itemInfo.getTHUMB().isEmpty()) {
            ImageUtils.loadImage(context, itemInfo.getTHUMB(), holder.imvMeetingPic);
        } else {
            if (itemInfo.getTYPE() != null) {
                if (itemInfo.getTYPE().equals("video")) {
                    holder.imvMeetingPic.setBackgroundResource(R.drawable.more_shipin);
                } else if (itemInfo.getTYPE().equals("ppt")) {
                    holder.imvMeetingPic.setBackgroundResource(R.drawable.more_ppt);
                } else if (itemInfo.getTYPE().equals("document")) {
                    holder.imvMeetingPic.setBackgroundResource(R.drawable.more_wenxian);
                }
            }
        }
//        ImageUtils.loadImage(context, itemInfo.getTHUMB(), holder.imvMeetingPic);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String date_start = "", date_stop = "";
        if (!TextUtils.isEmpty(itemInfo.getCREATE_AT())) {
            try {
                date_start = sdf.format(Date.valueOf(itemInfo.getCREATE_AT().substring(0, 9)));
//                holder.tvMeetingDate.setText(date_start);
                holder.tvSource.setText(date_start);
            } catch (Exception e) {
                e.printStackTrace();
                holder.tvSource.setText("待确定");
            }
        } else {
            holder.tvSource.setText("待确定");
        }



        holder.tvAuthor.setText(itemInfo.getDOCAUTHOR());
        holder.tvUnit.setText(itemInfo.getDOCCOMPANY());
        holder.tvTitle.setText(itemInfo.getTITLE());
        if (itemInfo.getTYPE().equals("conference")) {
            holder.tvState.setText("会议");
        } else if (itemInfo.getTYPE().equals("ppt")) {
            holder.tvState.setText("PPT");
        } else if (itemInfo.getTYPE().equals("video")) {
            holder.tvState.setText("视频");
        } else if (itemInfo.getTYPE().equals("document")) {
            holder.tvState.setText("文献");
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, collectionList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {

        return collectionList.size();
    }

    //item元素
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle, tvUnit, tvAuthor, tvSource, tvState;//会议标题，会议状态，会议时间，会议地址
        ImageView imvMeetingPic;//会议图片

        public ViewHolder(View itemView) {
            super(itemView);
            tvState = (TextView) itemView.findViewById(R.id.txt_item_state);
            tvTitle = (TextView) itemView.findViewById(R.id.txt_item_title);
            tvAuthor = (TextView) itemView.findViewById(R.id.txt_item_author);
            tvUnit = (TextView) itemView.findViewById(R.id.txt_item_unit);
            tvSource = (TextView) itemView.findViewById(R.id.txt_item_source);

            imvMeetingPic = (ImageView) itemView.findViewById(R.id.imv_meeting);
        }
    }
}
